import 'dart:async';
import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:device_apps/device_apps.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:yt_devops/utils/brn_row1_delegate.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';

class MapUtils {
  /// 高德地图
  static Future<bool> gotoAMap(String address, longitude, latitude) async {
    var url =
        '${Platform.isAndroid ? 'android' : 'ios'}amap://viewMap?sourceApplication=applicationName&poiname=$address&lat=$latitude&lon=$longitude&dev=0&dev=1';
    var uri = Uri.parse(url);

    bool canLaunchUrls = await canLaunchUrl(uri);

    if (!canLaunchUrls) {
      //失败的话会调用这里的方法
      return false;
    }

    await launchUrl(uri);

    return true;
  }

  /// 腾讯地图
  static Future<bool> gotoTencentMap(
      String address, longitude, latitude) async {
    var url =
        'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    var uri = Uri.parse(url);
    bool canLaunchUrls = await canLaunchUrl(Uri.parse(url));

    if (!canLaunchUrls) {
      return false;
    }

    await launchUrl(uri);

    return canLaunchUrls;
  }

  /// 百度地图
  static Future<bool> gotoBaiduMap(String address, longitude, latitude) async {
    var url =
        "baidumap://map/geocoder?location=$latitude,$longitude&src=andr.baidu.openAPIdemo";
    // 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=bd09ll&mode=driving';

    var uri = Uri.parse(url);
    bool canLaunchUrls = await canLaunchUrl(Uri.parse(url));

    if (!canLaunchUrls) {
      return false;
    }

    await launchUrl(uri);

    return canLaunchUrls;
  }

  /// 苹果地图
  static Future<bool> gotoAppleMap(String address, longitude, latitude) async {
    var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';

    var uri = Uri.parse(url);
    bool canLaunchUrls = await canLaunchUrl(Uri.parse(url));

    if (!canLaunchUrls) {
      return false;
    }

    await launchUrl(uri);

    return canLaunchUrls;
  }

  /// 谷歌地图
  static Future<bool> gotoGoogleMap(String address, longitude, latitude) async {
    // 谷歌地图URL模板，可以通过特定的纬度、经度和地址打开谷歌地图
    String googleMapsUrl =
        'comgooglemaps://?daddr=${Uri.encodeComponent(address)}&center=${latitude},${longitude}';
    var uri = Uri.parse(googleMapsUrl);
    // 尝试打开谷歌地图
    bool isOpened =
        await canLaunchUrl(uri) ? await launchUrl(uri) : throw '无法打开谷歌地图';

    if (!isOpened) {
      // 如果谷歌地图没有打开，可以尝试使用默认浏览器打开谷歌地图网页版
      String webUrl =
          'https://www.google.com/maps/dir/?api=1&destination=${Uri.encodeComponent(address)}&travelmode=driving';
      await launchUrl(Uri.parse(webUrl));
    }
    return isOpened;
  }

  static tryAllMap(
      BuildContext context, String address, longitude, latitude) async {
    // if (await gotoAMap(address, longitude, latitude) == true) {
    // } else
    List<Application> apps = await DeviceApps.getInstalledApplications();
    if (await gotoBaiduMap(address, longitude, latitude) == true) {
    } else if (await gotoTencentMap(address, longitude, latitude) == true) {
    } else if (await gotoAppleMap(address, longitude, latitude) == true) {
    } else {
      FuncUtils.requestTipsDislog(context, '无法打开第三方地图', false);
    }
  }

  static chooseMap(BuildContext context,
      {required String address, required longitude, required latitude}) {
    var list = getMapList();

    BrnMultiDataPicker(
      context: context,
      delegate: Brn1RowDelegate(
        list,
      ),
      confirmClick: (selectedIndexList) async {
        var map = list[selectedIndexList[0]];
        bool isLaunch = false;
        if (map == LaunchMapType.amap.label) {
          isLaunch = await gotoAMap(address, longitude, latitude);
        } else if (map == LaunchMapType.baiduMap.label) {
          isLaunch = await gotoBaiduMap(address, longitude, latitude);
        } else if (map == LaunchMapType.tencentMap.label) {
          isLaunch = await gotoTencentMap(address, longitude, latitude);
        } else if (map == LaunchMapType.appleMap.label) {
          isLaunch = await gotoAppleMap(address, longitude, latitude);
        } else if (map == LaunchMapType.googleMap.label) {
          isLaunch = await gotoGoogleMap(address, longitude, latitude);
        }
        if (!isLaunch) {
          FuncUtils.requestTipsDislog(context, '打开失败，请先安装$map', false);
        }
      },
    ).show();
  }

  static List<String> getMapList() {
    List<String> list = [
      LaunchMapType.amap.label,
      LaunchMapType.baiduMap.label,
      LaunchMapType.tencentMap.label,
    ];

    if (Platform.isIOS) {
      list.add(LaunchMapType.appleMap.label);
    }
    return list;
  }
}
